const express = require('express')
const router = express.Router()
const Classify = require('../models/classify.js')
const Article = require('../models/article.js')
const Merchat = require('../models/merchat.js')

router.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header('Access-Control-Allow-Credentials','true');
  next()
})

router.get('/classify/getClassifyByPid', (req, res, next) => {
  Classify.getList(req.query).then(data => {
    res.json({
      errcode: 200,
      errmsg: '调用成功',
      data
    })
  }).catch(err => {
    res.json(err)
  })
})

router.get('/article/getArticleList', (req, res, next) => {
  Article.getList(req.query).then(data => {
    res.json({
      errcode: 200,
      errmsg: '调用成功',
      data
    })
  }).catch(err => {
    res.json(err)
  })
})

router.get('/article/getArticleById', (req, res, next) => {
  Article.getDetail(req.query.id).then(data => {
    res.json({
      errcode: 200,
      errmsg: '调用成功',
      data
    })
  })
})

router.use((req, res, next) => {
  res.json({
    errcode: 404,
    errmsg: 'Not Found'
  })
})

module.exports = router
